package com.xjgc.msibase.config;

import lombok.Data;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author: 小逗号
 * @date: 2023/4/17
 * @description:
 * @since:
 */
@Data
//@ConfigurationProperties(prefix = "auto.sync.pool")
//@Configuration
//@EnableAsync
public class AutoSyncPoolConfig {
    /**
     * 核心线程数
     */
    private Integer corePoolSize;
    /**
     * 最大线程数
     */
    private Integer maxPoolSize;
    /**
     * 工作队列大小
     */
    private Integer queueCapatity;



    @ConditionalOnMissingBean(name = "autoSyncThread")
    @Bean(name = "autoSyncThread")
    public Executor threadPoolTaskExecutor(AutoSyncPoolConfig autoSyncPoolConfig) {
        System.out.println("autoSyncThread init ....");
        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        //核心线程数
        threadPoolTaskExecutor.setCorePoolSize(autoSyncPoolConfig.corePoolSize);
        //最大线程数
        threadPoolTaskExecutor.setMaxPoolSize(autoSyncPoolConfig.maxPoolSize);
        //工作队列大小
        threadPoolTaskExecutor.setQueueCapacity(autoSyncPoolConfig.queueCapatity);
        //线程名称前缀
        threadPoolTaskExecutor.setThreadNamePrefix("stockThreadPool-->");
        //设置拒绝策略.工作队列已满,线程数为最大线程数时,接收到新任务抛出异常
        threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
        //初始化线程池
        threadPoolTaskExecutor.initialize();
        return threadPoolTaskExecutor;
    }
}
